home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / GENCTXT.ZIP / CHAP4.TXT < prev    next >
Text File  |  1987-11-21  |  32KB  |  718 lines

  1.  
  2.                  Chapter 4 - Assignment & Logical compares
  3.  
  4.  
  5.              Throughout  this  chapter,  references  are  given   to
  6.         various  ranges  of  variables.  Your  compiler  may  use  a
  7.         different range for some of the variables since the proposed
  8.         ANSI  standard does not define specific limits for all  data
  9.         types.  Consult the documentation for your compiler for  the
  10.         exact range for each of the variable types.
  11.  
  12.                        INTEGER ASSIGNMENT STATEMENTS
  13.  
  14.              Load the file INTASIGN.C and display it for an  example
  15.         of  assignment statements.   Three variables are defined for
  16.         use  in the program and the rest of the program is merely  a
  17.         series of illustrations of various assignments.   The  first
  18.         two  lines  of  the assignment statements  assign  numerical
  19.         values  to "a" and "b",  and the next five lines  illustrate
  20.         the  five  basic arithmetic functions and how to  use  them.
  21.         The  fifth is the modulo operator and gives the remainder if
  22.         the two variables were divided.   It can only be applied  to
  23.         "int"  or  "char"  type  variables,   and  of  course  "int"
  24.         extensions such as "long",  "short",  etc.  Following these,
  25.         there  are two lines illustrating how to combine some of the
  26.         variables  in  some complex math expressions.   All  of  the
  27.         above examples should require no comment except to say  that
  28.         none  of the equations are meant to be  particularly  useful
  29.         except  as illustrations.  The "char" type variable will  be
  30.         defined in the description of the next example program.
  31.  
  32.              The  expressions  in  lines 17  and  18  are  perfectly
  33.         acceptable  as given, but we will see later in this  chapter
  34.         that  there is another way to write these for  more  compact
  35.         code.
  36.  
  37.              This leaves us with the last two lines which may appear
  38.         to  you  as being very strange.   The C compiler  scans  the
  39.         assignment  statement from right to left,  (which may seem a
  40.         bit odd since we do not read that way),  resulting in a very
  41.         useful construct,  namely the one given here.   The compiler
  42.         finds the value 20, assigns it to "c", then continues to the
  43.         left finding that the latest result of a calculation  should
  44.         be  assigned to "b".   Thinking that the latest  calculation
  45.         resulted in a 20,  it assigns it to "b" also,  and continues
  46.         the leftward scan assigning the value 20 to "a" also.   This
  47.         is a very useful construct when you are initializing a group
  48.         of  variables.   The last statement illustrates that  it  is
  49.         possible  to actually do some calculations to arrive at  the
  50.         value  which  will be assigned to all three  variables.   In
  51.         fact,  the rightmost expression can contain variables,  even
  52.         "a", "b", & "c".
  53.  
  54.              The  program has no output, so compiling and  executing
  55.         this  program  will be very uninteresting.  Since  you  have
  56.  
  57.  
  58.                                   Page 18
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.                  Chapter 4 - Assignment & Logical compares
  69.  
  70.  
  71.         already  learned how to display some integer  results  using
  72.         the "printf" function, it would be to your advantage to  add
  73.         some output statements to this program to see if the various
  74.         statements do what you think they should do.
  75.  
  76.              This would be a good time for a preliminary  definition
  77.         of  a  rule to be followed in C.   The data definitions  are
  78.         always given before any executable statements in any program
  79.         block.   This is why the variables are defined first in this
  80.         program and in every C program.  If you try to define a  new
  81.         variable after executing some statements, your compiler will
  82.         issue an error.
  83.  
  84.                            ADDITIONAL DATA TYPES
  85.  
  86.              Loading and editing MORTYPES.C will illustrate how some
  87.         additional  data  types can be used.   Once  again  we  have
  88.         defined  a  few integer type variables which you  should  be
  89.         fairly  familiar  with  by now,  but we have added  two  new
  90.         types, the "char", and the "float".
  91.  
  92.              The  "char"  type  of data is nearly the  same  as  the
  93.         integer except that it can only be assigned numerical values
  94.         between  -128 to 127 on most implementations of C, since  it
  95.         is  stored in only one byte of memory.  The "char"  type  of
  96.         data is usually used for ASCII data, more commonly known  as
  97.         text.  The text you are reading was originally written on  a
  98.         computer with a word processor that stored the words in  the
  99.         computer  one character per byte.  In contrast, the  integer
  100.         data  type  is  stored in two bytes of  computer  memory  on
  101.         nearly all microcomputers.
  102.  
  103.                               DATA TYPE MIXING
  104.  
  105.              It  would be profitable at this time to discuss the way
  106.         C handles the two types "char" and "int".  Most functions in
  107.         C  that are designed to operate with integer type  variables
  108.         will work equally well with character type variables because
  109.         they  are a form of an integer variable.   Those  functions,
  110.         when called on to use a "char" type variable,  will actually
  111.         promote  the "char" data into integer data before using  it.
  112.         For this reason, it is possible to mix "char" and "int" type
  113.         variables in nearly any way you desire.   The compiler  will
  114.         not get confused,  but you might.  It is good not to rely on
  115.         this too much, but to carefully use only the proper types of
  116.         data where they should be used.
  117.  
  118.               The second new data type is the "float" type of  data,
  119.         commonly  called floating point data.  This is a  data  type
  120.         which  usually  has a very large range, a  large  number  of
  121.         significant digits, and a large number of computer words are
  122.  
  123.  
  124.                                   Page 19
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.                  Chapter 4 - Assignment & Logical compares
  135.  
  136.  
  137.         required  to store it.  The "float" data type has a  decimal
  138.         point associated with it and has an allowable range of  from
  139.         3.4E-38   to  3.4E+38  when  using  most  C   compilers   on
  140.         microcomputers,  and  is  composed of  about  7  significant
  141.         digits.
  142.  
  143.                        HOW TO USE THE NEW DATA TYPES
  144.  
  145.              The  first three lines of the program assign values  to
  146.         all nine of the defined variables so we can manipulate  some
  147.         of the data between the different types.
  148.  
  149.              Since,  as  mentioned above,  a "char" data type is  in
  150.         reality  an "integer" data type,  no special  considerations
  151.         need be taken to promote a "char" to an "int",  and a "char"
  152.         type data field can be assigned to an "int" variable.   When
  153.         going the other way, there is no standard, so you may simply
  154.         get garbage if the value of the integer variable is  outside
  155.         the  range of the "char" type variable.   It will  translate
  156.         correctly if the value is within the range of -128 to 127.
  157.  
  158.              The   third   line   illustrates  the   simplicity   of
  159.         translating an integer into a "float",  simply assign it the
  160.         new  value  and the system will do  the  proper  conversion.
  161.         When  going  the  other  way  however,  there  is  an  added
  162.         complication.   Since  there may be a fractional part of the
  163.         floating  point number,  the system must decide what  to  do
  164.         with it.  By definitions , it will truncate it.
  165.  
  166.              This program produces no output, and we haven't covered
  167.         a way to print out "char" and "float" type variables, so you
  168.         can't  really  get  in  to this program and  play  with  the
  169.         results, but the next program will cover this for you.
  170.  
  171.              Compile  and  run  this program.
  172.  
  173.                           LOTS OF VARIABLE TYPES
  174.  
  175.              Load the file LOTTYPES.C and display it on your screen.
  176.         This  file contains nearly every standard simple  data  type
  177.         available  in the programming language C.  There  are  other
  178.         types,  but  they are the compound types (ie  -  arrays  and
  179.         structures) that we will cover in due time.
  180.  
  181.              Observe  the  file.  First we define  a  simple  "int",
  182.         followed by a "long int" which has a range of -2147483648 to
  183.         2147483647  with most C compilers, and a "short  int"  which
  184.         has  a  range  that  is identical  to  that  for  the  "int"
  185.         variable, namely -32768 to 32767. The "unsigned" is next and
  186.         is  defined as the same size as the "int" but with no  sign.
  187.         The  "unsigned" then will cover a range of 0 to  65535.   It
  188.  
  189.  
  190.                                   Page 20
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.                  Chapter 4 - Assignment & Logical compares
  201.  
  202.  
  203.         should  be  pointed out that when the  "long",  "short",  or
  204.         "unsigned" is desired, the "int" is optional and is left out
  205.         by  most experienced programmers.  We have  already  covered
  206.         the "char" and the "float", which leaves only the  "double".
  207.         The "double" covers a greater range than the "float" and has
  208.         more  significant digits for more precise calculations.   It
  209.         also  requires more memory to store a value than the  simple
  210.         "float". The "double" in most C compilers covers a range  of
  211.         1.7E-308 to 1.7E+308.
  212.  
  213.              Note that other compunding of types can be done such as
  214.         "long  unsigned  int",  "unsigned char",  etc.   Check  your
  215.         documentation for a complete list of variable types.
  216.  
  217.              Another  diversion  is in order at  this  point.   Your
  218.         compiler has no provisions for floating point math, but only
  219.         double floating point math.  It will promote a "float" to  a
  220.         "double"  before doing calculations and therefore  only  one
  221.         math  library  will be needed.  Of course, this  is  totally
  222.         transparent  to  you, so you don't need to worry  about  it.
  223.         Because  of  this, you may think that it would  be  best  to
  224.         simply define every floating point variable as double, since
  225.         they  are promoted before use in any calculations, but  that
  226.         may not be a good idea.  A "float" variable requires 4 bytes
  227.         of storage and a "double" requires 8 bytes of storage, so if
  228.         you have a large volume of floating point data to store, the
  229.         "double" will obviously require much more memory.
  230.  
  231.              After  defining  the data types in  the  program  under
  232.         consideration, a numerical value is assigned to each of  the
  233.         defined  variables  in  order to demonstrate  the  means  of
  234.         outputting each to the monitor.
  235.  
  236.                             THE CONVERSION CHARACTERS
  237.  
  238.              Following   is  a  list  of  some  of  the   conversion
  239.         characters  and  the  way  they are  used  in  the  "printf"
  240.         statement.   A  complete  list  of  all  of  the  conversion
  241.         characters  should  be included with the  documentation  for
  242.         your compiler.
  243.  
  244.              d    decimal notation
  245.              o    octal notation
  246.              x    hexadecimal notation
  247.              u    unsigned notation
  248.              c    character notation
  249.              s    string notation
  250.              f    floating point notation
  251.  
  252.  
  253.  
  254.  
  255.  
  256.                                   Page 21
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.                  Chapter 4 - Assignment & Logical compares
  267.  
  268.  
  269.              Each  of  these  is used following a  percent  sign  to
  270.         indicate  the type of output conversion,  and between  those
  271.         two characters, the following  fields may be added.
  272.  
  273.              -    left justification in its field
  274.              (n)  a number specifying minimum field width
  275.              .    to separate n from m
  276.              (m)  significant fractional digits for a float
  277.              l    to indicate a "long"
  278.  
  279.              These  are all used in the examples which are  included
  280.         in the program presently displayed on your monitor, with the
  281.         exception of the string notation which will be covered later
  282.         in  this tutorial. Note especially the variable field  width
  283.         specification  demonstrated in lines 33 to 36.  This is  not
  284.         part of the original definition of C, but it is included  in
  285.         the  proposed  ANSI standard and will become part of  the  C
  286.         language.   Compile and run this program to see what  effect
  287.         the various fields have on the output.
  288.  
  289.              You  now  have the ability to display any of  the  data
  290.         fields  in  the  previous programs and it would be  to  your
  291.         advantage to go back and see if you can display some of  the
  292.         fields anyway you desire.
  293.  
  294.                               LOGICAL COMPARES
  295.  
  296.              Load  and  view  the file  named  COMPARES.C  for  many
  297.         examples  of compare statements in C.   We begin by defining
  298.         and  initializing  nine variables to use  in  the  following
  299.         compare  statements.   This initialization is new to you and
  300.         can be used to initialize variables while they are defined.
  301.  
  302.              The  first  group of compare statements represents  the
  303.         simplest  kinds  of compares since they simply  compare  two
  304.         variables.    Either  variable  could  be  replaced  with  a
  305.         constant and still be a valid compare,  but two variables is
  306.         the general case.  The first compare checks to see if "x" is
  307.         equal  to  "y"  and it uses the double equal  sign  for  the
  308.         comparison.   A single equal sign could be used here but  it
  309.         would have a different meaning as we will see shortly.   The
  310.         second  comparison checks to see if "x" is greater than "z".
  311.  
  312.              The  third compare introduces the "NOT"  operator,  the
  313.         exclamation,  which can be used to invert the result of  any
  314.         logical  compare.   The fourth checks for "b" less  than  or
  315.         equal to "c", and the last checks for "r" not equal to  "s".
  316.         As  we  learned in the last chapter, if the  result  of  the
  317.         compare  is  true, the statement following the  "if"  clause
  318.         will be executed and the results are given in the  comments.
  319.  
  320.  
  321.  
  322.                                   Page 22
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332.                  Chapter 4 - Assignment & Logical compares
  333.  
  334.  
  335.         Note  that  "less than" and "greater than or equal  to"  are
  336.         also available, but are not illustrated here.
  337.  
  338.              It  would be well to mention the different format  used
  339.         for the "if" statement in this example program.   A carriage
  340.         return  is  not  required as a statement  separator  and  by
  341.         putting the conditional clause on the same line as the "if",
  342.         it adds to the readability of the overall program.
  343.  
  344.                                MORE COMPARES
  345.  
  346.              The  compares  in  the  second group  are  a  bit  more
  347.         involved.  Starting with the first compare, we find a rather
  348.         strange  looking set of conditions in the  parentheses.   To
  349.         understand  this  we must understand just what a  "true"  or
  350.         "false"  is in the C language.   A "false" is defined  as  a
  351.         value  of zero,  and "true" is defined as a non-zero  value.
  352.         Any  integer  or char type of variable can be used  for  the
  353.         result of a true/false test, or the result can be an implied
  354.         integer or char.
  355.  
  356.              Look  at  the  first compare of  the  second  group  of
  357.         compare  statements.  The expression "r != s" will  evaluate
  358.         as  a "true" since "r" was set to 0.0 above, so  the  result
  359.         will  be a non-zero value.  With most C compilers, it  would
  360.         always  be set to a 1, but you could get in trouble  if  you
  361.         wrote  a program that depended on it being 1 in  all  cases.
  362.         Good programming practice would be to not use the  resulting
  363.         1  in any calculations.  Even though the two variables  that
  364.         are  compared are "float" variables, the result will  be  of
  365.         type  "integer".  There is no explicit variable to which  it
  366.         will be assigned so the result of the compare is an  implied
  367.         integer.   Finally the resulting number, probably 1 in  this
  368.         case,  is assigned to the integer variable "x".   If  double
  369.         equal signs were used, the phantom value, namely 1, would be
  370.         compared  to  the value of "x", but since the  single  equal
  371.         sign  is  used, the value 1 is simply assigned  to  "x",  as
  372.         though  the  statement were not  in  parentheses.   Finally,
  373.         since  the result of the assignment in the  parentheses  was
  374.         non-zero, the entire expression is evaluated as "true",  and
  375.         "z" is assigned the value of 1000.  Thus we accomplished two
  376.         things  in  this  statement, we assigned "x"  a  new  value,
  377.         probably  1,  and  we assigned "z" the value  of  1000.   We
  378.         covered a lot in this statement so you may wish to review it
  379.         before  going on.  The important things to remember are  the
  380.         values  that  define "true" and "false", and the  fact  that
  381.         several  things can be assigned in a conditional  statement.
  382.         The  value  assigned to "x" was probably a 1,  but  remember
  383.         that the only requirement is that it is nonzero.
  384.  
  385.  
  386.  
  387.  
  388.                                   Page 23
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.  
  398.                  Chapter 4 - Assignment & Logical compares
  399.  
  400.  
  401.              The next example should help clear up some of the above
  402.         in your mind.  In this example, "x" is assigned the value of
  403.         "y",  and since the result is 11, the condition is non-zero,
  404.         which is true, and the variable "z" is assigned 222.
  405.  
  406.              The third example, in the second group, compares "x" to
  407.         zero.   If  the result is true,  meaning that if "x" is  not
  408.         zero,  then "z" is assigned the value of 333,  which it will
  409.         be.   The  last  example in this group illustrates the  same
  410.         concept,  since the result will be true if "x" is  non-zero.
  411.         The compare to zero is not actually needed and the result of
  412.         the compare is true.   The third and fourth examples of this
  413.         group are therefore identical.
  414.  
  415.                         ADDITIONAL COMPARE CONCEPTS
  416.  
  417.              The   third  group  of  compares  will  introduce  some
  418.         additional  concepts,  namely  the  logical  "AND"  and  the
  419.         logical  "OR".   We  assign  the value of 77  to  the  three
  420.         integer  variables  simply to get started  again  with  some
  421.         defined  values.   The  first  compare of  the  third  group
  422.         contains  the new control "&&",  which is the logical "AND".
  423.         The  entire statement reads,  if "x" equals "y" AND  if  "x"
  424.         equals  77 then the result is "true".   Since this is  true,
  425.         the variable z is set equal to 33.
  426.  
  427.              The  next  compare  in this group introduces  the  "||"
  428.         operator which is the "OR".   The statement reads, if "x" is
  429.         greater  than  "y"  OR if "z" is greater than  12  then  the
  430.         result is true.   Since "z" is greater than 12,  it  doesn't
  431.         matter  if "x" is greater than "y" or not,  because only one
  432.         of  the  two conditions must be true for the  result  to  be
  433.         true.  The result is true, so therefore "z" will be assigned
  434.         the value of 22.
  435.  
  436.                              LOGICAL EVALUATION
  437.  
  438.              When a compound expression is evaluated, the evaluation
  439.         proceeds from left to right and as soon as the result of the
  440.         outcome is assured,  evaluation stops.   Namely, in the case
  441.         of  an "AND" evaluation,  when one of the terms evaluates to
  442.         "false",  evaluation is discontinued because additional true
  443.         terms  cannot make the result ever become  "true".   In  the
  444.         case of an "OR" evaluation,  if any of the terms is found to
  445.         be  "true",  evaluation stops because it will be  impossible
  446.         for additional terms to cause the result to be "false".   In
  447.         the case of additionally nested terms,  the above rules will
  448.         be applied to each of the nested levels.
  449.  
  450.  
  451.  
  452.  
  453.  
  454.                                   Page 24
  455.  
  456.  
  457.  
  458.  
  459.  
  460.  
  461.  
  462.  
  463.  
  464.                  Chapter 4 - Assignment & Logical compares
  465.  
  466.  
  467.                           PRECEDENCE OF OPERATORS
  468.  
  469.              The  question will come up concerning the precedence of
  470.         operators.   Which  operators are evaluated first and  which
  471.         last?   There are many rules about this topic, but  I  would
  472.         suggest  that  you  don't  worry about  it  at  this  point.
  473.         Instead,  use  lots  of  parentheses  to  group   variables,
  474.         constants,  and  operators  in  a  way  meaningful  to  you.
  475.         Parentheses always have the highest priority and will remove
  476.         any  question of which operations will be done first in  any
  477.         particular statements.
  478.  
  479.              Going  on to the next example in group three,  we  find
  480.         three  simple variables used in the conditional part of  the
  481.         compare.   Since  all  three  are non-zero,  all  three  are
  482.         "true",  and therefore the "AND" of the three variables  are
  483.         true,  leading  to  the result being "true",  and "z"  being
  484.         assigned  the value of 11.   Note that since the  variables,
  485.         "r", "s", and "t" are "float" type variables, they could not
  486.         be  used this way,  but they could each be compared to  zero
  487.         and the same type of expression could be used.
  488.  
  489.              Continuing on to the fourth example of the third  group
  490.         we  find three assignment statements in the compare part  of
  491.         the "if" statement.  If you understood the above discussion,
  492.         you  should have no difficulty understanding that the  three
  493.         variables are assigned their respective new values,  and the
  494.         result  of  all three are non-zero,  leading to a  resulting
  495.         value of "TRUE".
  496.  
  497.                         THIS IS A TRICK, BE CAREFUL
  498.  
  499.              The last example of the third group contains a bit of a
  500.         trick, but since we have covered it above, it is nothing new
  501.         to you.  Notice that the first part of the compare evaluates
  502.         to  "FALSE".   The  remaining parts of the compare  are  not
  503.         evaluated,  because it is an "AND" and it will definitely be
  504.         resolved as a "FALSE" because the first term is  false.   If
  505.         the program was dependent on the value of "y" being set to 3
  506.         in  the  next  part of the compare,  it  will  fail  because
  507.         evaluation  will  cease following the "FALSE" found  in  the
  508.         first  term.   Likewise,  "z" will not be set to 4,  and the
  509.         variable "r" will not be changed.
  510.  
  511.                           POTENTIAL PROBLEM AREAS
  512.  
  513.              The   last   group   of   compares   illustrate   three
  514.         possibilities for getting into a bit of trouble.   All three
  515.         have  the  common  result that "z" will not get set  to  the
  516.         desired value,  but for different reasons.   In the case  of
  517.         the  first  one,  the compare evaluates as "true",  but  the
  518.  
  519.  
  520.                                   Page 25
  521.  
  522.  
  523.  
  524.  
  525.  
  526.  
  527.  
  528.  
  529.  
  530.                  Chapter 4 - Assignment & Logical compares
  531.  
  532.  
  533.         semicolon  following the second parentheses  terminates  the
  534.         "if"  clause,  and the assignment statement involving "z" is
  535.         always executed as the next statement.   The "if"  therefore
  536.         has  no  effect  because of the  misplaced  semicolon.   The
  537.         second  statement is much more straightforward  because  "x"
  538.         will  always  be equal to itself,  therefore the  inequality
  539.         will never be true, and the entire statement will never do a
  540.         thing, but is wasted effort.  The last statement will always
  541.         assign  0  to "x" and the compare will therefore  always  be
  542.         "false",  never  executing the conditional part of the  "if"
  543.         statement.
  544.  
  545.              The  conditional  statement is extremely important  and
  546.         must be thoroughly understood to write efficient C programs.
  547.         If  any  part of this discussion is unclear  in  your  mind,
  548.         restudy  it  until you are confident that you understand  it
  549.         thoroughly before proceeding onward.
  550.  
  551.              Compile and run this program.  Add some printout to see
  552.         the results of some of the operations.
  553.  
  554.                            THE CRYPTIC PART OF C
  555.  
  556.              There are three constructs used in C that make no sense
  557.         at   all  when  first  encountered  because  they  are   not
  558.         intuitive,  but they greatly increase the efficiency of  the
  559.         compiled  code  and  are used extensively by  experienced  C
  560.         programmers.   You  should therefore be exposed to them  and
  561.         learn to use them because they will appear in most,  if  not
  562.         all,  of the programs you see in the publications.  Load and
  563.         examine  the file named CRYPTIC.C for examples of the  three
  564.         new constructs.
  565.  
  566.              In  this  program,   some  variables  are  defined  and
  567.         initialized in the same statements for use below.  The first
  568.         executable statement simply adds 1 to the value of "x",  and
  569.         should come as no surprise to you.   The next two statements
  570.         also  add one to the value of "x",  but it is not  intuitive
  571.         that this is what happens.   It is simply by definition that
  572.         this is true.  Therefore, by definition of the C language, a
  573.         double   plus  sign  either  before  or  after  a   variable
  574.         increments  that variable by 1.   Additionally,  if the plus
  575.         signs are before the variable,  the variable is  incremented
  576.         before  it  is used,  and if the plus signs  are  after  the
  577.         variable,  the variable is used,  then incremented.   In the
  578.         next statement, the value of "y" is assigned to the variable
  579.         "z",  then  "y"  is incremented because the plus  signs  are
  580.         after  the  variable  "y".   In the last  statement  of  the
  581.         incrementing  group of example statements,  the value of "y"
  582.         is  incremented then its value is assigned to  the  variable
  583.         "z".
  584.  
  585.  
  586.                                   Page 26
  587.  
  588.  
  589.  
  590.  
  591.  
  592.  
  593.  
  594.  
  595.  
  596.                  Chapter 4 - Assignment & Logical compares
  597.  
  598.  
  599.  
  600.              The  next group of statements illustrate decrementing a
  601.         variable by one.   The definition works exactly the same way
  602.         for decrementing as it does for incrementing.   If the minus
  603.         signs are before the variable,  the variable is decremented,
  604.         then  used,  and if the minus signs are after the  variable,
  605.         the variable is used, then decremented.
  606.  
  607.                       THE CRYPTIC ARITHMETIC OPERATOR
  608.  
  609.              Another  useful but cryptic operator is the  arithmetic
  610.         operator.   This operator is used to modify any variable  by
  611.         some constant value.  The first statement of the "arithmetic
  612.         operator" group of statements simply adds 12 to the value of
  613.         the variable "a".   The second statement does the same,  but
  614.         once again, it is not intuitive that they are the same.  Any
  615.         of the four basic functions of arithmetic, "+", "-", "*", or
  616.         "/",  can  be handled in this way,  by putting the  function
  617.         desired  in  front  of the equal sign  and  eliminating  the
  618.         second  reference to the variable name.   It should be noted
  619.         that  the  expression on the right side  of  the  arithmetic
  620.         operator can be any valid expression,  the examples are kept
  621.         simple for your introduction to this new operator.
  622.  
  623.              Just  like the incrementing and decrementing operators,
  624.         the arithmetic operator is used extensively by experienced C
  625.         programmers and it would pay you well to understand it.
  626.  
  627.                          THE CONDITIONAL EXPRESSION
  628.  
  629.              The  conditional expression is just as cryptic  as  the
  630.         last  two,  but once again it can be very useful so it would
  631.         pay you to understand it.   It consists of three expressions
  632.         within parentheses separated by a question mark and a colon.
  633.         The  expression prior to the question mark is  evaluated  to
  634.         determine  if it is "true" or "false".   If it is true,  the
  635.         expression  between  the  question mark  and  the  colon  is
  636.         evaluated,  and if it is not true,  the expression following
  637.         the  colon  is evaluated.   The result of the evaluation  is
  638.         used for the assignment.   The final result is identical  to
  639.         that  of an "if" statement with an "else" clause.   This  is
  640.         illustrated  by  the  second example  in  this  group.   The
  641.         conditional  expression  has  the added  advantage  of  more
  642.         compact code that will compile to fewer machine instructions
  643.         in the final program.
  644.  
  645.              The  final two lines of this example program are  given
  646.         to  illustrate  a very compact way to assign the greater  of
  647.         two variables "a" or "b" to "c", and to assign the lessor of
  648.         the  same two variables to "c".   Notice how  efficient  the
  649.         code is in these two examples.
  650.  
  651.  
  652.                                   Page 27
  653.  
  654.  
  655.  
  656.  
  657.  
  658.  
  659.  
  660.  
  661.  
  662.                  Chapter 4 - Assignment & Logical compares
  663.  
  664.  
  665.  
  666.                      TO BE CRYPTIC OR NOT TO BE CRYPTIC
  667.  
  668.              Several students of C have stated that they didn't like
  669.         these  three  cryptic constructs and that they would  simply
  670.         never  use  them.  This will be fine if they never  have  to
  671.         read  anybody  else's  program, or use  any  other  programs
  672.         within their own.  I have found many functions that I wished
  673.         to  use within a program but needed a small modification  to
  674.         use  it, requiring me to understand another  person's  code.
  675.         It  would therefore be to your advantage to learn these  new
  676.         constructs, and use them. They will be used in the remainder
  677.         of this tutorial, so you will be constantly exposed to them.
  678.  
  679.              This has been a long chapter but it contained important
  680.         material  to  get  you  started in using  C.   In  the  next
  681.         chapter,  we  will go on to the building blocks  of  C,  the
  682.         functions.  At that point, you will have enough of the basic
  683.         materials to allow you to begin writing meaningful programs.
  684.  
  685.  
  686.         PROGRAMMING EXERCISES
  687.  
  688.         1.   Write  a program that will count from 1 to 12 and print
  689.              the count, and its square, for each count.
  690.                   1    1
  691.                   2    4
  692.                   3    9   etc.
  693.  
  694.         2.   Write a program that counts from 1 to 12 and prints the
  695.              count  and  its  inversion  to  5  decimal  places  for
  696.              each count. This will require a floating point number.
  697.                   1    1.00000
  698.                   2     .50000
  699.                   3     .33333
  700.                   4     .25000
  701.                   etc.
  702.  
  703.         3.   Write a program that will count from 1 to 100 and print
  704.              only those values between 32 and 39, one to a line.
  705.  
  706.  
  707.  
  708.  
  709.  
  710.  
  711.  
  712.  
  713.  
  714.  
  715.  
  716.  
  717.                                   Page 28
  718.